﻿using Dapper;
using Domain.Entity.In;
using Domain.Entity.Out;
using Domain.IRepository;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repository
{
    public class Outstock_dRepository : IOutstock_dRepository
    {

        //连接字符串
        string con = "Data Source=121.37.93.71;Initial Catalog=CSSL_ZCTWMS;User ID=sa;Password=geili@2025;Encrypt=False";

        //添加出库明细
        public async Task<int> AddOutstock_d(Outstock_d outstock_D)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"insert into [dbo].[out_outstock_d](slot_id, out_id, proc_id, proc_name, slot_name, outd_numtotal, outd_price, outd_num, outd_outstockOK_num, outd_outstockNO_num, outd_total, proc_date, remark)
                                values({outstock_D.slot_id},{outstock_D.out_id},{outstock_D.proc_id},'{outstock_D.proc_name}','{outstock_D.slot_name}',{outstock_D.outd_numtotal},'{outstock_D.outd_price}',{outstock_D.outd_num},{outstock_D.outd_outstockOK_num},{outstock_D.outd_outstockNO_num},'{outstock_D.outd_total}','{outstock_D.proc_date}','{outstock_D.remark}')";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }

        //删除出库明细
        public async Task<int> DeleteOutstock_d(int outdid)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"delete from [dbo].[out_outstock_d] where outd_id={outdid}";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }


        //根据出库单id查询明细
        public async Task<List<Outstock_d>> GetOutstock_Ds(int outstockid)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $"select * from [dbo].[out_outstock_d] where out_id={outstockid}";
                return sqlConnection.Query<Outstock_d>(sql).ToList();
            }
        }


        //修改出库明细
        public async Task<int> UpdateOutstock_d(Outstock_d outstock_D)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"update [dbo].[out_outstock_d] set slot_id={outstock_D.slot_id},out_id={outstock_D.out_id},proc_id={outstock_D.proc_id},proc_name='{outstock_D.proc_name}',slot_name='{outstock_D.slot_name}',outd_price='{outstock_D.outd_price}',outd_num={outstock_D.outd_num},
                                outd_outstockOK_num={outstock_D.outd_outstockOK_num},outd_outstockNO_num={outstock_D.outd_outstockNO_num},outd_total='{outstock_D.outd_total}',proc_date='{outstock_D.proc_date}',remark='{outstock_D.remark}' where outd_id={outstock_D.outd_id}";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }
    }
}
